package utils

import (
	"github.com/kataras/iris/v12"
	"strings"
)

// GetClientIP 获取http请求的客户端ip地址
func GetClientIP(ctx iris.Context) string {
	var ip string
	realIps := ctx.Request().Header.Get("X-Forwarded-For")
	if realIps != "" && len(realIps) != 0 && !strings.EqualFold("unknown", realIps) {
		ipArray := strings.Split(realIps, ",")
		ip = ipArray[0]
	}
	if ip == "" {
		ip = ctx.Request().Header.Get("Proxy-Client-IP")
	}
	if ip == "" {
		ip = ctx.Request().Header.Get("WL-Proxy-Client-IP")
	}
	if ip == "" {
		ip = ctx.Request().Header.Get("HTTP_CLIENT_IP")
	}
	if ip == "" {
		ip = ctx.Request().Header.Get("HTTP_X_FORWARDED_FOR")
	}
	if ip == "" {
		ip = ctx.Request().Header.Get("X-Real-IP")
	}
	if ip == "" {
		ip = strings.Split(ctx.Request().RemoteAddr, ":")[0]
	}
	return ip
}

// GetUserAgent 获取user-agent
func GetUserAgent(ctx iris.Context) string {
	return ctx.Request().Header.Get("User-Agent")
}
